import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;



public class dobriKvazigrupi {

	public LinkedList<String> goodQuasi= new LinkedList<>();
	public LinkedList<Integer> periodi = new LinkedList<>();
	
	public void getGoodQuasigroup (String file) throws IOException{
		
		BufferedReader read=new BufferedReader(new FileReader(file));
		BufferedWriter write=new BufferedWriter(new FileWriter("DobriKvazigrupi.txt"));
		
		String tmp="";
		String quasi="";
		int perioda;
		//String niza[];
		
		while ((tmp=read.readLine())!=null){
			System.out.println(tmp);
			
			//niza=tmp.split(" ");
			//quasi=niza[0];
			perioda=Integer.parseInt(tmp);//niza[1]);
			write.write(quasi+"\n");
			goodQuasi.add(tmp);
			if(perioda>=12285){
				write.write(quasi+ " "+perioda+"\n");
				goodQuasi.add(quasi);
				//periodi.add(perioda);
			}
		}
		
		read.close();
		write.close();

	}
	
	
	public void getGoodPolinoms() throws IOException{
		
		BufferedReader r1=new BufferedReader(new FileReader("polinomi.txt"));
		BufferedWriter write1=new BufferedWriter(new FileWriter("polinomi\\polinomi1.txt"));
/*		BufferedWriter write2=new BufferedWriter(new FileWriter("polinomi\\polinomi2.txt"));
		BufferedWriter write3=new BufferedWriter(new FileWriter("polinomi\\polinomi3.txt"));
		BufferedWriter write4=new BufferedWriter(new FileWriter("polinomi\\polinomi4.txt"));
		BufferedWriter write5=new BufferedWriter(new FileWriter("polinomi\\polinomi5.txt"));
		BufferedWriter write6=new BufferedWriter(new FileWriter("polinomi\\polinomi6.txt"));
*/		
		String tmp="";
		String quasi="";
		String niza[];
		

		int i=0;
		while((tmp=r1.readLine())!=null){
			
			niza=tmp.split(" ");
			quasi=niza[0];
			if(goodQuasi.contains(quasi)){
				i++;
//				if(i<=5){
					
					write1.write(quasi+"\n");
					write1.write(r1.readLine()+"\n");
					write1.write(r1.readLine()+"\n");
					write1.write(r1.readLine()+"\n");

/*				}
				if(i>5 && i<=10){

					write2.write(quasi+"\n");
					write2.write(r1.readLine()+"\n");
					write2.write(r1.readLine()+"\n");
					write2.write(r1.readLine()+"\n");
				}
				if(i>10 && i<=25){

					write3.write(quasi+"\n");
					write3.write(r1.readLine()+"\n");
					write3.write(r1.readLine()+"\n");
					write3.write(r1.readLine()+"\n");
				}
				if(i>25 && i<=40){
	
					write4.write(quasi+"\n");
					write4.write(r1.readLine()+"\n");
					write4.write(r1.readLine()+"\n");
					write4.write(r1.readLine()+"\n");
				}
				if(i>40 && i<=55){
					
					write5.write(quasi+"\n");
					write5.write(r1.readLine()+"\n");
					write5.write(r1.readLine()+"\n");
					write5.write(r1.readLine()+"\n");
				}
				if(i>55){
					write6.write(quasi+"\n");
					write6.write(r1.readLine()+"\n");
					write6.write(r1.readLine()+"\n");
					write6.write(r1.readLine()+"\n");
				}
*/			}
		}
		
		r1.close();
		write1.close();
		/*write2.close();
		write3.close();
		write4.close();
		write5.close();
		write6.close();*/
	}
	
	
	public static void main(String args[]) throws IOException{
		
		dobriKvazigrupi d=new dobriKvazigrupi();
		d.getGoodQuasigroup("odbrani kvazigrupi.txt");
		
		d.getGoodPolinoms();
		
	}
}
